// vr/vr-list/vr-list.js

const formatNumber = (n) => {
    n = n.toString();
    return n[1] ? n : "0" + n;
};

function dateToStr(date) {
    return date.getFullYear() + '-' + formatNumber(date.getMonth() + 1) + '-' + formatNumber(date.getDate())
}

const dayTime = 60 * 60 * 24 * 1000

Page({
    /**
     * 页面的初始数据
     */
    data: {
        params: {
            page: 1,
            pageSize: 10,
        },
        searchData: {
            title: '',
            start_time: dateToStr(new Date(new Date().getTime() - (dayTime * 7))),
            end_time: dateToStr(new Date())
        },
        dateSelect: {
            show: false,
            type: '',
            currentDate: new Date().getTime()
        },
        hasMore: false,
        orderList: [],
    },

    handleGetMore() {
        const {
            params,
            hasMore
        } = this.data;

        if (!hasMore) {
            params.page++;

            this.setData({
                params,
            });

            this.getData();
        }
    },
    searchData() {
        const {
            params
        } = this.data;

        params.page = 1;

        this.setData({
            orderList: [],
            params,
        });

        this.getData();
    },

    getData() {
        const {
            params,
            searchData,
        } = this.data;

        wx.ajax
            .post("querPageList.action", {
                ...params,
                ...searchData
            })
            .then((res) => {
                if (res.success) {
                    const result = res.result;
                    const originalList = this.data.orderList;

                    this.setData({
                        orderList: originalList.concat(result.pageList || []),
                    });

                    if (result.page === result.totalPages) {
                        this.setData({
                            hasMore: true,
                        });
                    }
                }
            });
    },

    showDatePicker(event) {
        const {
            type
        } = event.currentTarget.dataset

        let dateSelect = {
            show: true,
            type,
        }

        if (type == 'start') {
            dateSelect.currentDate = new Date(this.data.searchData.start_time).getTime()
        } else {
            dateSelect.currentDate = new Date(this.data.searchData.end_time).getTime()
        }

        this.setData({
            dateSelect
        })
    },

    hideDatePicker(event) {
        this.setData({
            'dateSelect.show': false
        })
    },

    confirmDate(event) {
        const {
            dateSelect,
            searchData
        } = this.data
        let setData = {
            'dateSelect.show': false,
        }
        if (dateSelect.type == 'start') {
            setData['searchData.start_time'] = dateToStr(new Date(event.detail))
        } else {
            setData['searchData.end_time'] = dateToStr(new Date(event.detail))
        }

        this.setData(setData, () => {
            // this.searchData()
        });
    },

    titleInput(e) {
        this.setData({
            'searchData.title': e.detail.value
        })
    },

    addHandle() {
        wx.navigateTo({
            url: "/vr/pages/vr-form/vr-form",
        });
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {},

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.searchData()
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {},

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {},

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {},

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {},

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {},
});